<template>
    <div>
      <h1>Register</h1>
      <form @submit.prevent="handleRegister">
        <input v-model="user.userName" type="text" placeholder="用户名" autocomplete="username" />
        <input v-model="user.userEmail" type="email" placeholder="邮箱" />
        <input v-model="user.userPassword" type="password" placeholder="密码" autocomplete="new-password" />
        <button type="submit">Register</button>
      </form>
    </div>
  </template>
  
  <script>
  import { register } from '@/api/user';
  import { setToken } from '@/utils/storage';
  
  export default {
    data() {
      return {
        user: {
          userName: '',
          userPassword: '',
          userEmail: '',
        },
      };
    },
    methods: {
      async handleRegister() {
        try {
          const response = await register(this.user);
          // 存储 token
          setToken(response.data.token);
          // 跳转到登录页面
          this.$router.push('/profile/login');
        } catch (error) {
          console.error('Registration failed:', error);
        }
      },
    },
  };
  </script>